<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Official_Order extends CI_Controller {

    public function Official_Order() {
        parent::__construct();
        //$this -> load -> model('admin_model');
        $this->load->model('user_model');
        $this->load->model('official_order_model');
        $this->load->library('administrator_authenticate');
    }

    public function index($option = null) {
        $this->load->model('ministry_model');
        $this->load->model('official_status_model');
        $this->load->model('official_status_model');
        $this->load->model('medal_type_model');
        $this->load->model('medal_class_model');
        $this->load->model('medal_model');
        $this->load->model('officer_model');
        $this->administrator_authenticate->check_login();
        $data['content'] = 'official_order_view';
        $data['header'] = 'ការទទួលមេដាយតាមព្រះរាជក្រឹត្យ';
        $data['title_official'] = 'តារាងព្រះរាជាក្រឹត្យ';
        $data['jquery_ui'] = '';
        $data['order_type'] = '1';
        $data['jquery_dialog'] = '';
        $data['ministries'] = $this->ministry_model->get_ministry();
        $data['official_statuses'] = $this->official_status_model->get_official_status();
        $data['medal_types'] = $this->medal_type_model->get_medal_type();
        $data['medal_classes'] = $this->medal_class_model->get_medal_class();
        $data['medals'] = $this->medal_model->getMedalTypeClass();
        $data['officers'] = $this->officer_model->getOfficer();
        // option =1 or other : normal case
        // option =2 : status = តាមដាន
        $data['option'] = empty($option) ? 1 : $option;
        $this->load->view('admin_template', $data);
    }

    public function index_secondary($option = null) {
        $this->load->model('ministry_model');
        $this->load->model('official_status_model');
        $this->load->model('official_status_model');
        $this->load->model('medal_type_model');
        $this->load->model('medal_class_model');
        $this->load->model('medal_model');
        $this->load->model('officer_model');
        $this->administrator_authenticate->check_login();
        $data['content'] = 'official_order_view';
        $data['header'] = 'ការទទួលមេដាយតាមអនុក្រឹត្យ';
        $data['title_official'] = 'តារាងអនុក្រឹត្យ';
        $data['jquery_ui'] = '';
        $data['order_type'] = '2';
        $data['jquery_dialog'] = '';
        $data['ministries'] = $this->ministry_model->get_ministry();
        $data['official_statuses'] = $this->official_status_model->get_official_status();
        $data['medal_types'] = $this->medal_type_model->get_medal_type();
        $data['medal_classes'] = $this->medal_class_model->get_medal_class();
        $data['medals'] = $this->medal_model->getMedalTypeClass();
        $data['officers'] = $this->officer_model->getOfficer();

        // option =1 or other : normal case
        // option =2 : status = តាមដាន
        $data['option'] = empty($option) ? 1 : $option;

        $this->load->view('admin_template', $data);
    }

    public function search() {
        $this->administrator_authenticate->check_login();
        $output = $this->official_order_model->get_official_order();
        echo json_encode($output);
    }

    public function add_official_order() {
        $this->administrator_authenticate->check_login();
        $return = array();

        if (!empty($_FILES)) {
            $upload = $this->_do_upload('officialfile');
        } else {
            $upload['error'] = '';
            $upload['success']['upload_data']['file_name'] = '';
        }
        if (trim(@$_POST['officialid']) != "") { // UPDATE
            if (empty($_FILES)) {
                $saved = $this->official_order_model->update();
            } else if ($upload['error'] == '') {
                $file_name = $upload['success']['upload_data']['file_name'];
                $saved = $this->official_order_model->update($file_name);
            } else {
                $return['result'] = false;
                $return['message'] = $upload['error']['error'];
                echo json_encode($return);
                exit;
            }
            if ($saved['result']) {
                $return['result'] = true;
            } else {
                $return['result'] = false;
            }
            $return['message'] = $saved['message'];
        } else { // INSERT
            if ($upload['error'] == '') {
                $file_name = $upload['success']['upload_data']['file_name'];
                $saved = $this->official_order_model->checkExistingOfficialNumber($file_name);
                if ($saved['result']) {
                    $return['result'] = true;
                } else {
                    $return['result'] = false;
                }
                $return['message'] = $saved['message'];
            } else {
                $return['result'] = false;
                $return['message'] = $upload['error']['error'];
            }
        }
        echo json_encode($return);
    }

    public function add_official_medal_Order() {
        $this->administrator_authenticate->check_login();
        $this->load->model('medal_model');
        $this->load->model('official_medal_order_model');
        $result = $this->official_medal_order_model->add();
        echo json_encode($result);
    }

    public function add_medal_reception() {
        $this->administrator_authenticate->check_login();
        $this->load->model("medal_reception_model");
        $return = array();

//        $totalReceipt = $this->medal_reception_model->totalReceipt();  // Total Receipt
//        $receiptTaken = $this->medal_reception_model->ReceiptTaken(); // Receipt Taken
//        if (($totalReceipt - $receiptTaken) > 0) {
        $return = $this->medal_reception_model->add();
//        } else {
//            $return['result'] = false;
//            $return['message'] = 'មិនមានមេដាយគ្រប់គ្រាន់ដើម្បីបញ្ចូលទេ';
//        }
        echo json_encode($return);
    }

    public function _do_upload($file_name) {
        $this->administrator_authenticate->check_login();
        $result = array('error' => '', 'success' => '');
        $config['upload_path'] = './public/files/';
        $config['allowed_types'] = 'pdf';
        $config['max_size'] = '1024';

        $this->load->library('upload', $config);
        if (!$this->upload->do_upload($file_name)) {
            $result['error'] = array('error' => $this->upload->display_errors());
        } else {
            $result['success'] = array('upload_data' => $this->upload->data());
        }
        return $result;
    }

    public function delete_official_order() {
        $this->administrator_authenticate->check_login();
        $this->official_order_model->deleteOfficialOrder();
        $return['result'] = true;
        $return['message'] = 'លុបទិន្នន័យបានជោគជ័យ';
        echo json_encode($return);
    }

    public function delete_official_medal_order() {
        $this->administrator_authenticate->check_login();
        $this->load->model('official_medal_order_model');
        $this->official_medal_order_model->deleteOfficialMedalOrder();
        $return['result'] = true;
        $return['message'] = 'លុបមេដាយដែលត្រូវការបានជោគជ័យ.';
        echo json_encode($return);
    }

    public function delete_medal_reception() {
        $this->administrator_authenticate->check_login();
        $this->load->model('medal_reception_model');
        $this->medal_reception_model->deleteMedalReception();
        $return['result'] = true;
        $return['message'] = 'លុបមន្ត្រីទទួលមេដាយបានជោគជ័យ.';
        echo json_encode($return);
    }

    public function official_medal_order() {
        $this->administrator_authenticate->check_login();
        $this->load->model('official_medal_order_model');
        $output = $this->official_medal_order_model->get_official_medal_order();
        echo json_encode($output);
    }

    public function medal_reception() {
        $this->administrator_authenticate->check_login();
        $this->load->model("medal_reception_model");
        $output = $this->medal_reception_model->get_medal_reception();
        echo json_encode($output);
    }

    public function read_official_Order() {
        $this->administrator_authenticate->check_login();
        $output = $this->official_order_model->read();
        echo json_encode($output[0]);
    }

    public function official_list() {
        $this->administrator_authenticate->check_login();
        $Officers = $this->official_order_model->getOfficial(check_get('term'));
        echo json_encode($Officers);
    }
}